package com.runa.rccprotocol.dto.jntmp;

import com.runa.protocol.dto.DtuReportAck;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class JingNengAck extends DtuReportAck {

    public JingNengAck(int cmd, String addr, int firmware) {
        this.addr = addr;
        LocalDateTime now = LocalDateTime.now();
        this.data = new byte[]{0x57, 0x63, 0x53, 0x6A, 13, 0, (byte) cmd, 0, (byte) firmware,
                (byte) (now.getYear() & 0x0ff), (byte) (now.getYear() >> 8 &0xff),
                (byte) now.getMonthValue(), (byte) now.getDayOfMonth(),
                (byte) now.getHour(), (byte) now.getMinute(), (byte) now.getSecond(),
                0, 0, 0, 0, 0};
        ByteUtil.intToByte((int) now.toEpochSecond(ZoneOffset.of("+8")), data,16, ByteOrder.DCBA);
        data[data.length - 1] = ByteUtil.xorSum(data, 0, data.length - 1);
    }

    public JingNengAck(int cmd, String addr, int offset, int size, byte[] firmware) {
        this.addr = addr;
        this.data = new byte[]{0x57, 0x63, 0x53, 0x6A, 0, 0, (byte) cmd};
        ByteUtil.shortToByte((short) (6 + firmware.length), data, 4, ByteOrder.BA);
        byte[] bytes = new byte[6];
        ByteUtil.shortToByte((short) offset, bytes, 0, ByteOrder.BA);
        ByteUtil.shortToByte((short) size, bytes, 2, ByteOrder.BA);
        ByteUtil.shortToByte((short) firmware.length, bytes, 4, ByteOrder.BA);
        data = ByteUtil.merge(data, bytes, firmware, new byte[1]);
        data[data.length - 1] = ByteUtil.xorSum(data, 0, data.length - 1);
    }

}
